class LeaveMgrController {
    def authenticateService
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.sort = 'id'
        params.order = 'desc'
        [leaveInstanceList: Leave.findAllByApprover(authenticateService?.userDomain() , params), 
            leaveInstanceTotal: Leave.countByApprover(authenticateService?.userDomain())]
    }

    def pass = {
        def leaveInstance = Leave.get(params.id)
        if (!leaveInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leave.label', default: 'Leave'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [leaveInstance: leaveInstance ]
        }
    }

    def update = {
        def leaveInstance = Leave.get(params.id)
        if (leaveInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (leaveInstance.version > version) {
                    
                    leaveInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'leave.label', default: 'Leave')] as Object[], "Another user has updated this Leave while you were editing")
                    render(view: "pass", model: [leaveInstance: leaveInstance ,  approvers : approvers()])
                    return
                }
            }
            leaveInstance.properties = params
            if (!leaveInstance.hasErrors() && leaveInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'leave.label', default: 'Leave'), leaveInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "pass", model: [leaveInstance: leaveInstance ,  approvers : approvers()])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leave.label', default: 'Leave'), params.id])}"
            redirect(action: "list")
        }
    }
}
